package myservlet.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mybean.data.login;

public class changeDistract extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
			super.init(config);
			try {//加载驱动
				Class.forName("com.mysql.jdbc.Driver");
				//System.out.println("加载驱动成功");				
			}
			catch(Exception e) {}
		}
	public  void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		String distract = req.getParameter("distract");//获取用户提交的地区名
		if(distract==null) distract="";
		HttpSession session=req.getSession(true);
		Connection con=null;
		login loginBean = null;
		try {
			loginBean = (login)session.getAttribute("loginBean");
			boolean b=loginBean.getLogname()==null || loginBean.getLogname().length()==0;
			if(b) {
				out.println("<script language = javascript>alert('请重新登录!');");
				out.println("location.href='index.jsp'</script>");
				return;
			}
			String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true";
			Statement sql;
			ResultSet rs;
			//修改单车所在地区
			try {//连接数据库
				con = DriverManager.getConnection(url,"root","744615");
				String Dno="",Bid="";
				String condition="select B_id from orderform where username='"+loginBean.getLogname()+"'";//获取已借的单车编号
				sql=con.createStatement();
				rs=sql.executeQuery(condition);
				if(rs.next()) {
					Bid=rs.getString("B_id");
				}
				else{
					out.println("<script language = javascript>alert('没有借用车，不能修改地区!');");
					out.println("location.href='return.jsp'</script>");
				}
				condition="select * from district where Dname='"+distract+"'";//获取该地区的编号
				sql=con.createStatement();
				rs=sql.executeQuery(condition);
				while(rs.next()) {
					Dno=rs.getString("Dno");
				}
				condition="update bike set B_district='"+Dno+"' where B_id='"+Bid+"'";//修改单车所属地区
				sql.executeUpdate(condition);
				sql.close();
				out.println("<script language = javascript>alert('地区修改成功，请完成还车操作!');");
				out.println("location.href='return.jsp'</script>");
			}
			catch (SQLException exp){
				System.out.println(exp);
				out.println("<script language = javascript>alert('地区修改失败!');");
				out.println("location.href='return.jsp'</script>");
			}
		}
		catch (Exception exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('地区修改失败!');");
			out.println("location.href='return.jsp'</script>");
		}
	}	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}
